class Queue{
    constructor(){
        this.items = {}; //队列的元素
        this.count = 0;  //记录键
        this.lowestCount = 0; //删除元素键
    }

    //入队
    enqueue(element){
        this.items[this.count] = element;
        this.count++;
    }

    //删除
    dequeue(){
        let target = this.items[this.lowestCount];
        delete this.items[this.lowestCount];
        this.lowestCount++;
        return target
    }

    //获取队列的元素个数
    size(){
        return this.count - this.lowestCount
    }

    //判断队列是否为空
    isEmpty(){
        return this.count - this.lowestCount === 0
    }

    //返回第一个元素
    peek(){
        return this.items[this.lowestCount];
    }

    //清空队列
    clear(){
        this.items = {};
        this.count = 0;
        this.lowestCount = 0;
    }

    //转字符串
    toString(){
        if(this.isEmpty()){
            return ''
        }
        let str = this.items[this.lowestCount];

        for(let i = this.lowestCount+1;i<this.count;i++){
            str = `${str},${this.items[i]}`;
        }

        return str
    }
}

// let d = new Queue();

// d.enqueue('a');

// d.enqueue('b');

// d.enqueue('c');

// d.enqueue('d');

// d.dequeue();

// console.log(d.toString());

//击鼓传花
function flower(member,num){
    let d = new Queue();
    let list = [];
    //1.入队
    for(let i = 0;i<member.length;i++){
        d.enqueue(member[i]);
    }

    //2.报数删除 保留一个人
    while(d.size() > 1){
        for(let j = 0;j<num-1;j++){
            d.enqueue(d.dequeue()) //
        }
        list.push(d.dequeue())
    }

    let reduce = d.dequeue();

    return {
        reduce,
        list
    }
}

console.log(flower(['a','b','c','d'],3))
